<!-- XHTML 1.0 Strict -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<meta name="author" content="Rachid Touzani" />
<meta name="keywords" content=" css, dropdowns, dropdown menu, drop-down, menu, navigation, nav, horizontal, vertical left-to-right, vertical right-to-left, horizontal linear, horizontal upwards, cross browser, internet explorer, ie, firefox, safari, opera, browser, lwis" />
<meta name="description" content="Clean, standards-friendly, modular framework for dropdown menus" />
<link href="../css/dropdown.vertical.css" media="screen" rel="stylesheet" type="text/css" />
<link href="../css/default.ultimate.css" media="screen" rel="stylesheet" type="text/css" />

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Heat Transfer Demo 2</title>
<link rel="stylesheet" type="text/css" href="../doxygen.css" />
<link rel="stylesheet" type="text/css" href="../tabs.css"  />
</head>

<body bgcolor="#FFFFFF" link="#FF0000" vlink="#FF00FF" alink="#FF00FF">
<p align="center"><a href="../index.html"><img src="../im/ofeli.gif" width="150" border="0"></a></p>

<center>
  <div id="cse" style="width: 60%;">Loading</div>
  <script src="http://www.google.com/jsapi" type="text/javascript"></script>
  <script type="text/javascript"> 
     google.load('search', '1', {language : 'en', style : google.loader.themes.SHINY});
     google.setOnLoadCallback(function() {
        var customSearchOptions = {};  var customSearchControl = new google.search.CustomSearchControl(
        '012396140824982761142:-qrywxcfi_o', customSearchOptions);
        customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
        customSearchControl.draw('cse');
     }, true);
   </script>
   <style type="text/css">
     .gsc-control-cse {
     font-family: Verdana, sans-serif;
     border-color: #DAE0E5;
     background-color: #DAE0E5;
   }
   .gsc-control-cse .gsc-table-result { font-family: Verdana, sans-serif; }
   input.gsc-input { border-color: #B6BEC5; }
   input.gsc-search-button {
     border-color: #B6BEC5;
     background-color: #D0D1D4;
   }
   .gsc-tabHeader.gsc-tabhInactive {
     border-color: #999999;
     background-color: #EEEEEE;
   }
   .gsc-tabHeader.gsc-tabhActive {
     border-color: #999999;
     background-color: #999999;
   }
   .gsc-tabsArea { border-color: #999999; }
   .gsc-webResult.gsc-result,
   .gsc-results .gsc-imageResult {
     border-color: #FFFFFF;
     background-color: #FFFFFF;
   }
   .gsc-webResult.gsc-result:hover,
   .gsc-imageResult:hover {
     border-color: #D2D6DC;
     background-color: #EDEDED;
   }
   .gsc-webResult.gsc-result.gsc-promotion:hover {
     border-color: #D2D6DC;
     background-color: #EDEDED;
   }
   .gs-webResult.gs-result a.gs-title:link,
   .gs-webResult.gs-result a.gs-title:link b,
   .gs-imageResult a.gs-title:link,
   .gs-imageResult a.gs-title:link b { color: #0568CD; }
   .gs-webResult.gs-result a.gs-title:visited,
   .gs-webResult.gs-result a.gs-title:visited b,
   .gs-imageResult a.gs-title:visited,
   .gs-imageResult a.gs-title:visited b { color: #0568CD; }
   .gs-webResult.gs-result a.gs-title:hover,
   .gs-webResult.gs-result a.gs-title:hover b,
   .gs-imageResult a.gs-title:hover,
   .gs-imageResult a.gs-title:hover b { color: #0568CD; }
   .gs-webResult.gs-result a.gs-title:active,
   .gs-webResult.gs-result a.gs-title:active b,
   .gs-imageResult a.gs-title:active,
   .gs-imageResult a.gs-title:active b { color: #0568CD; }
   .gsc-cursor-page { color: #0568CD; }
   a.gsc-trailing-more-results:link { color: #0568CD; }
   .gs-webResult .gs-snippet,
   .gs-imageResult .gs-snippet,
   .gs-fileFormatType { color: #5F6A73; }
   .gs-webResult div.gs-visibleUrl,
   .gs-imageResult div.gs-visibleUrl { color: #5F6A73; }
   .gs-webResult div.gs-visibleUrl-short { color: #5F6A73; }
   .gs-webResult div.gs-visibleUrl-short { display: none; }
   .gs-webResult div.gs-visibleUrl-long { display: block; }
   .gs-promotion div.gs-visibleUrl-short { display: none; }
   .gs-promotion div.gs-visibleUrl-long { display: block; }
   .gsc-cursor-box { border-color: #FFFFFF; }
   .gsc-results .gsc-cursor-box .gsc-cursor-page {
      border-color: #999999;
      background-color: #FFFFFF;
      color: #0568CD;
   }
   .gsc-results .gsc-cursor-box .gsc-cursor-current-page {
      border-color: #999999;
      background-color: #999999;
      color: #0568CD;
   }
   .gsc-webResult.gsc-result.gsc-promotion {
      border-color: #D2D6DC;
      background-color: #D0D1D4;
   }
   .gsc-completion-title { color: #0568CD; }
   .gsc-completion-snippet { color: #5F6A73; }
   .gs-promotion a.gs-title:link,
   .gs-promotion a.gs-title:link *,
   .gs-promotion .gs-snippet a:link { color: #0066CC; }
   .gs-promotion a.gs-title:visited,
   .gs-promotion a.gs-title:visited *,
   .gs-promotion .gs-snippet a:visited { color: #0066CC; }
   .gs-promotion a.gs-title:hover,
   .gs-promotion a.gs-title:hover *,
   .gs-promotion .gs-snippet a:hover { color: #0066CC; }
   .gs-promotion a.gs-title:active,
   .gs-promotion a.gs-title:active *,
   .gs-promotion .gs-snippet a:active { color: #0066CC; }
   .gs-promotion .gs-snippet,
   .gs-promotion .gs-title .gs-promotion-title-right,
   .gs-promotion .gs-title .gs-promotion-title-right *  { color: #333333; }
   .gs-promotion .gs-visibleUrl,
   .gs-promotion .gs-visibleUrl-short { color: #5F6A73; }
   </style>
</center>
</p>
&nbsp;

<div id="mainmenu">
<div class="text">

 <div class="tabs">
   <ul class="tablist">
   <li><a href="../index.html"><span>Home</span></a>
   <li><a href="../overview.html"><span>Overview</span></a>
   <li><a href="../html/index.html"><span>Class Documentation</span></a>
   <li><a href="../fformats.html"><span>File Formats</span></a>
   <li><a href="../tutorial.html"><span>Tutorial</span></a>
   <li class="current"><a href="../demos.html"><span>Demo Codes</span></a>
 </div>

 </div>
 </div>

&nbsp;
<p>
<h2>Heat Transfer Demo 2: A 2-D transient diffusion code</h2>

<p><table border="2" cellpadding="2" cellspacing="0" >
<tr>
<td align="center" width="35"><a href="stdc2.html"><img src="../im/backward.gif" border="0"></a></td>
<td align="center" width="35"><a href="../demos.html"><img src="../im/top.gif" border="0"></a></td>
<td align="center" width="35"><a href="std3.html"><img src="../im/forward.gif" border="0"></a></td>
</tr>
</table></p>

<p>
<SPAN class=TEXT>
This code is developed to solve 2-D transient (time dependent) heat transfer problems.
We use here an alternative to the presentation of the previous demo code. More specifically,
we show how to perform all important phases of a time dependent finite element code.
</SPAN>

<ul type="square">
<li><SPAN class=TEXT>As usual, the program starts by including the header file <span class=VAR>OFELI.h</span> that itself includes all kernel
class definitions, then we include thermal dedicated classes described in <span class=VAR>Therm.h</span>.
We also declare the namespace to simplify the code writing:</span><br>
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>#include "OFELI.h"
#include "Therm.h"
using namespace OFELI;
</font></pre>
</td></tr></table></p>

<li><SPAN class=TEXT>We construct an instance of class <span class=VAR><a href="../html/classOFELI_1_1IPF.html">IPF</a></span> to manage project data.
We extract useful information from this instance: The maximal time, the time step, a flag for saving results and
another for controlling output.
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>   IPF data("ttd2 - 1.1",argv[1]);
   theFinalTime = data.getMaxTime();
   theTimeStep = data.getTimeStep();
   int save_flag = data.getSave();
   int verbose = data.getVerbose();</font></pre>
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
We instantiate class <span class=VAR><a href="../html/classOFELI_1_1IOField.html">IOField</a></span> to store the solution for plotting:
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>   IOField pf(data.getPlotFile(),IOField::OUT);</font></pre>
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
We now define the <span class=VAR><a href="../html/classOFELI_1_1Mesh.html">Mesh</a></span> instance and the matrix 
(class <span class=VAR><a href="../html/classOFELI_1_1SkSMatrix.html">SkSMatrix&lt;double&gt;</a></span> handles
skyline symmetric storage). We next instantiate vectors <span class=VAR>b</span> and <span class=VAR>u</span>
that will contain respectively the right-hand side and the solution.
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>   Mesh ms(data.getMeshFile());
   SkSMatrix&lt;double&gt; A(ms);
   Vect&lt;double&gt; b(ms), u(ms);
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
The initial solution is given through the class <span class=VAR><a href="../html/classOFELI_1_1Prescription.html">Prescription</a></span>.
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>   Prescription pr(ms,data.getDataFile());
   pr.get(INITIAL_FIELD,u);</font></pre>
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
Boundary conditions and source vector will also be defined in the same way, but
since all these properties are time dependent, we will do this each time step.
For now, we just instantiate these vectors and define a NodeVect instance.
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>   Vect&lt;double&gt; bc(ms), body_f(ms), bound_f(ms);</font></pre>
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
We start the time step loop using the macro <span class=tt>TimeLoop</span>:
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>   TimeLoop {
      b = 0;</font></pre>
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
For the current time step, we get (Dirichlet) boundary conditions, sources and fluxes (Neumann boundary conditions):
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>      pr.get(BOUNDARY_CONDITION,bc,time);
      pr.get(SOURCE,body_f,time);
      pr.get(FLUX,bound_f,time);</font></pre>
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
We define a loop over mesh elements to fill element arrays. We use here the macro <span class=VAR>MeshElements</span>
that stands for a shorthand of the element loop. It uses the mesh instance and gives a value to the global pointer
<span class=VAR>theElement</span> to class <span class=VAR><a href="../html/classOFELI_1_1Element.html">Element</a></span>:
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>      MeshElements(ms) {
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
For each element, we first define an instance of class <span class=VAR><a href="../html/classOFELI_1_1DC2DT3.html">
DC2DT3</a></span> which handles diffusion-convection problems in 2-D domains using 3-node triangular finite elements
(P<sub>1</sub> elements). We make use of the constructor for time dependent problems, this one involving the solution
at previous time step and the current value of time. We then
add the (lumped) capacity contribution to the element matrix and right-hand side. Note that
this contribution is multiplied by the inverse of the time step. We also add a contribution of the diffusion
matrix. All this corresponds to an implementation of the  backward Euler scheme. We finally add the <i>body right-hand side</i>:
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>         DC2DT3 eq(theElement,u,theTime);
         eq.LCapacity(double(1./theTimeStep));
         eq.Diffusion();
         eq.BodyRHS(body_f);</font></pre>
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
Once element arrays are computed, we assemble them to the global matrix and right-hand side. Note that since
the matrix is computed and factorized only once, we avoid reassembling it.
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>         if (step==1)
            eq.ElementAssembly(A);
         eq.ElementAssembly(b);
      }</font></pre>
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
When Neumann boundary conditions are concerned (fluxes), we have to add a loop over sides. Such a loop is very similar
to element loops.
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>      MeshSides(ms) {
         DC2DT3 eq(theSide,u,theTime);
         eq.BoundaryRHS(Vect&lt;double&gt;(theSide,bound_f));
         eq.SideAssembly(b);
      }</font></pre>
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
Once the linear system is constructed, we impose (Dirichlet) boundary conditions by a penalty technique.
This is done by the member function <span class=VAR>Prescribe</span> that belongs to all matrix classes.
We then solve the linear system by factorization and backsubstitution. The obtained solution is contained in
<span class=VAR>b</span> and transferred to vector <span class=VAR>u</span>.
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>      A.Prescribe(b,bc,theStep-1);
      A.solve(b);
      u = b;</font></pre>
</td></tr></table>
</SPAN></p>

<li><SPAN class=TEXT>
It remains now to store the solution in a file if the parameter <span class=VAR>save_flag</span>
enables it. The procedure it to store the solution each multiple of <span class=VAR>save_flag</span>
step.
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>      u.setTime(theTime);
      if (theStep%save_flag == 0)
         pf.put(u);
   }</font></pre>
</td></tr></table></P>
Note that we have stored in the solution vector the time value, so
that this one can be retrieved for plotting.

</SPAN></p>

&nbsp;&nbsp;
<h2>An example</h2>

<SPAN class=TEXT>
Let us run this program with the data presented in the following project file:
<p><table bgcolor="#e0f8f7" border="1" frame=box rules=none width=700>
<tr><td>
<pre><font color="blue" size=3>&lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
&lt;OFELI_File&gt;
&lt;info&gt;
    &lt;title&gt;Finite Element Mesh of a beam&lt;/title&gt;
    &lt;date&gt;January 1, 2010&lt;/date&gt;
    &lt;author&gt;R. Touzani&lt;/author&gt;
&lt;/info&gt;
&lt;Project name="proj"&gt;
   &lt;mesh_file&gt;proj-5x10.m&lt;/mesh_file&gt;
   &lt;plot_file&gt;proj.pl&lt;/plot_file&gt;
   &lt;time_step&gt;0.01&lt;/time_step&gt;
   &lt;max_time&gt;1.0&lt;/max_time&gt;
   &lt;verbose&gt;0&lt;/verbose&gt;
   &lt;output&gt;0&lt;/output&gt;
   &lt;save&gt;1&lt;/save&gt;
&lt;/Project&gt;
&lt;Prescription&gt;
   &lt;BoundaryCondition code="1"&gt;-tanh(10)*(exp(t)-1)&lt;/BoundaryCondition&gt;
   &lt;BoundaryCondition code="2"&gt; tanh(10)*(exp(t)-1)&lt;/BoundaryCondition&gt;
   &lt;Source&gt;tanh(10*y)*(exp(t)+200*(exp(t)-1)/(cosh(10*y)*cosh(10*y)))&lt;/Source&gt;
&lt;/Prescription&gt;
&lt;/OFELI_File&gt;</font></pre>
</td></tr></table></p>

<p>In summary, this file looks mainly like the one in the previous example. We show here
in addition how to prescribe a boundary condition by an algebraic expression. The 
<span class=LOGO>OFELI</span> library is equipped with the expression parser <span class=VAR>fparser</span>.
We use here the variables <span class=VAR>x</span>, <span class=VAR>y</span>, <span class=VAR>z</span>, 
and <span class=VAR>t</span> for space and time variables.
</span>

<p><table align="center" border="2" cellpadding="2" cellspacing="0" >
<tr>
<td align="center" width="35"><a href="stdc2.html"><img src="../im/backward.gif" border="0"></a></td>
<td align="center" width="35"><a href="../demos.html"><img src="../im/top.gif" border="0"></a></td>
<td align="center" width="35"><a href="std3.html"><img src="../im/forward.gif" border="0"></a></td>
</tr>
</table>

&nbsp;
<div id="foot_bar">
        Copyright &copy; 1998-2018 Rachid Touzani&nbsp;
</div>

</body>
</html>
